﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.Specialized;
using PBDesk.Business.Gigya;
using PBDesk.Utilities;
using System.Web.Security;
using PBDesk.DAL;
using System.Collections;
using CMS.PBDesk.Framework.Web;
using System.Diagnostics;
using System.Web;

namespace CMS.PBDesk.Business.UserManager
{
    public class LogonHelper
    {
        public static LogonStatus LogonGigyaUser(NameValueCollection qsParameters)
        {
            LogonStatus result = LogonStatus.Failure;
            if (qsParameters != null && qsParameters.Count > 0)
            {
                try
                {
                    GigyaUser gUser = GigyaHelper.GetGigyaUserFromQS(qsParameters);
                    if (gUser != null)
                    {
                        int gigyaInsUpdStatus = GigyaHelper.InsertOrUpdateGigyaUser(gUser);
                        Trace.WriteLine("gigyaInsUpdStatus" + gigyaInsUpdStatus.ToString());
                        if (gigyaInsUpdStatus == 1 || gigyaInsUpdStatus == 2)
                        {
                            string registeredUserName = GigyaHelper.IsGigyaUserRegistered(gUser.UID);
                            Trace.WriteLine("registeredUserName" + registeredUserName);
                            if(CommonHelper.IsValidUserName(registeredUserName))
                            {
                                result =  LogonStatus.Success;
                                Trace.WriteLine("Redirecting to default page");
                                CookieManager.WriteCookie(CookieHelper.COOKIE_USER, CookieHelper.COOKIE_USER_NAME, registeredUserName);
                                FormsAuthentication.RedirectFromLoginPage(registeredUserName, false);
                            }
                            else
                            {
                                result = LogonStatus.NewUser;
                            }
                            
                        }
                        
                            

                        
                        //MembershipCreateStatus membershipStatus;
                        //Trace.Write("a"); aa += "a";
                        //MembershipUser user = Membership.CreateUser(gUser.UID, "Y#zx8A72^d", "abc@abc.com", null, null, true, gUser.provider + "~" + gUser.loginProviderUID, out membershipStatus);
                        //Trace.Write("b"); aa += "b";
                        //if (membershipStatus == MembershipCreateStatus.DuplicateUserName)
                        //{
                        //    Trace.Write("c"); aa += "c";
                        //    user = Membership.GetUser(gUser.UID, false);
                        //    Trace.Write("d");
                        //}
                        //Trace.Write("e"); aa += "d";
                        //result1 = user.ProviderUserKey.ToString();

                    }
                }
                catch (Exception ex)
                {
                    throw (ex);
                    
                }
            }
            return result;
        }

        public static void Logout()
        {
            FormsAuthentication.SignOut();
            // delete cookies
            if (HttpContext.Current != null)
            {
                HttpContext.Current.Response.Redirect("/Home.aspx");
            }

        }
      

    }
}
